from app.outdoor.models import Activity, ActivityType
from common.authCheck.userAuth import check_login
from common.dictInfo import model_to_dict
from common.paginator import paginator
from django.http import JsonResponse
from rest_framework.views import APIView


class ActivityTypeActivityInfoView(APIView):
    INCLUDE_FIELDS = [
        'name', 'status', 'activity_type', 'sign_up_start_time', 'group_name'
    ]

    @check_login
    def get(self, request, typeID):
        """
        根据活动类型获取户外活动列表
        :param request:
        :param typeID:类型id
        :return:jsonResponse
        """
        activityType = ActivityType.objects.filter(id=typeID)
        if not activityType.exists():
            return JsonResponse({
                'status': False,
                'errMsg': '分类不存在'
            }, status=404)
        activityType = activityType[0]
        filterActivity = Activity.objects.filter(activity_type=activityType)
        pageActivity = paginator(filterActivity, request.GET.get('page'))
        activityDict = [model_to_dict(fa, fields=self.INCLUDE_FIELDS) for fa in pageActivity]
        return JsonResponse({
            'status': True,
            'activity_list': activityDict,
            'has_previous': pageActivity.has_previous(),
            'has_next': pageActivity.has_next()
        })
